package es.xpt.ysf.flow;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import com.thoughtworks.xstream.XStream;

public class FlowControllerTestReader {

	Flow flow;
	XStream xstr;
	InputStream input;

	public FlowControllerTestReader(String inputFile) {
		try {
			input = new FileInputStream(inputFile);
		} catch (FileNotFoundException fnfe) {
			System.err.println("File not found: " + inputFile);
		}
	}

	public void run() {
		configureXStream();
		flow = (Flow) xstr.fromXML(input);
		for (State s : flow.getStates()) {
			System.out.println("State: " + s);
			if (s.getActions() != null)
				for (Action a : s.getActions()) {
					if (a != null) {
						System.out.println("  Action: " + a);
						System.out.println("    Target: " + a.getNextState());
						System.out.println("    Method: " + a.getMethod());
					}
				}
		}
	}

	private void configureXStream() {
		xstr = new XStream();
		// xstr.alias("flow", Flow.class);
		// xstr.alias("state", State.class);
		// xstr.alias("action", Action.class);
		// xstr.addImplicitCollection(Flow.class, "states");
		// xstr.addImplicitCollection(State.class, "actions");
		// xstr.useAttributeFor(State.class, "name");
		// xstr.useAttributeFor(Action.class, "name");
	}

	public static void main(String[] args) {
		FlowControllerTestReader test = new FlowControllerTestReader(args[0]);
		test.run();
	}

}
